package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.Image;
import com.jgsu.farm.entity.MatchImage;
import com.jgsu.farm.entity.Video;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.exception.ImageException;
import com.jgsu.farm.mapper.ImageMapper;
import com.jgsu.farm.mapper.MatchImageMapper;
import com.jgsu.farm.mapper.sql.ImageMapperSql;
import com.jgsu.farm.mapper.sql.MatchImageMapperSql;
import com.jgsu.farm.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
;import java.util.*;

@Service
public class ImageServiceImpl implements ImageService {
    @Autowired
    private ImageMapperSql imageMapperSql;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    MatchImageMapper matchImageMapper;
    @Autowired
    MatchImageMapperSql matchImageMapperSql;


    @Override
    public List<Integer> addImageList(List<Image> images) {
        if (images.size() == 0) {
            return null;
        }
        List<Integer> list = new ArrayList<>();
        Iterator<Image> iterator = images.iterator();
        int imageid;
        Image image;
        while (iterator.hasNext()) {
            image = iterator.next();
            imageMapperSql.insert(image);
            imageid = image.getImageid();
            list.add(imageid);
        }
        return list;
    }

    @Override
       public Map<String, String> selectImageFileByImageID(List<Integer> list) {
           if (list.size() == 0) {
               return null;
           }
           Map<String, String> map = new HashMap<>();
           Iterator<Integer> iterator = list.iterator();
           Integer imageId;
           String imageFile;
           while (iterator.hasNext()) {
               imageId = iterator.next();
               imageFile = imageMapperSql.selectImageFileById(imageId);
               map.put(imageId + "", imageFile);
           }
           return map;
       }




    /**
     * 图片添加的方法
     */
    @Override
    public void addImage(Image image) {
        int insertOne = imageMapper.insert(image);
        if (insertOne != 1) {
            throw new DataAddException("604", "上传图片出现错误");
        }
    }

    /**
     * 删除
     * */

    @Override
    public void deleteImage(Integer imageid) {
        MatchImage matchImage = matchImageMapperSql.selectByImageId(imageid);
        if (matchImage == null){
            throw new DataNotExistException("603", "不存在该imageid,match_image的资源删除失败");
        }
        matchImageMapperSql.deleteByMatchIdAndImageId(matchImage.getMatchid(),imageid);
        int i = imageMapper.deleteByPrimaryKey(imageid);
        if (i == 0) {
            throw new DataNotExistException("603", "不存在该id,图片资源删除失败");
        }
    }

    /**
     * 更新
     * */

    @Override
    public void updateImage(Image image) {
        Image imageId = imageMapper.selectByPrimaryKey(image.getImageid());
        if (imageId == null) {
            throw new DataNotExistException("603", "不存在该id,图片资源更新失败");
        } else {
            imageMapper.updateByPrimaryKey(image);
        }
    }

    /**
     * 分页查询
     * */
    @Override
    public PageInfo<Image> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Image> imageList = imageMapper.selectAll();
        PageInfo<Image> pageInfoImageList = new PageInfo<Image>(imageList);
        return pageInfoImageList;
    }

}
